package util.cleaner;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;

import fileloaders.parsers.InLineParser;

import puppy.graph.DeliciousEdgeSimple;
import puppy.graph.DeliciousNode;

import udf.contants.Contants;

import util.io.FileInput;

public class GenerateCoOccuranceGraph {


	static private int _id_url = 0;
	static private int _id_tag = 1;
	static private int _url = 2;
	static private int _tag = 3;
	static private int _freq_u = 5;
	static private int _freq_tag = 4;
	static private int _freq_ut = 6;
	static private int _age = 7;
	
	private static int _adult_freq_tag= 8;
	private static int _adult_freq_url=9;
	private static int _adult_domain=10; // this was changed from 10 to 11
	private static int _adult_freq_domain=11;
	
	int offset= 10000000;
	
	
	//static Hashtable<String,Hashtable<String, ArrayList<DeliciousEdgeSimple >>> url_tags =  new Hashtable<String,Hashtable<String, ArrayList<DeliciousEdgeSimple >>> ();
	static Hashtable<String,Hashtable<String, DeliciousEdgeSimple >> tag_tag =  new Hashtable<String,Hashtable<String, DeliciousEdgeSimple >> ();
	
	
	public static void trimByFreq(String path) {

		FileInput in = new FileInput(path);
		String line = in.readString();
		
		
		// add url ids

		while (line != null) {

			String[] fields = line.split("\t");

			
			Hashtable<String, Object> elements = InLineParser.parseBigramEnrichedLine(fields);
		
			if(elements ==null) {
				continue;
			}
			
			String [] keys = (String[]) elements.get("keys");
			elements.remove("keys");
			DeliciousEdgeSimple edge= new DeliciousEdgeSimple();
			Enumeration<String> ages = elements.keys();
			
		
			String a=  keys[0];
			String b=  keys[1];
			
			while(ages.hasMoreElements()){
				String age = ages.nextElement();
				edge.freq.put(age, (Integer)elements.get(age));
				
			}
			
			
			DeliciousNode tag = null;
		/*	if (nodes.containsKey(name)) {
				tag = nodes.get(name);
				tag.setFreq(freq_tag, age);
				nodes.put(name, tag);

			} else {
				tag = new DeliciousNode();
				tag.setFreq(freq_tag, age);
				tag.setName(name);
			//	tag.setId(id_tag);
				tag.setType(false);

				nodes.put(name, tag);
				tag.setFreq(adult_freq_tag, "adults");

			}
*/
			
			
			if (a.compareTo(b) < 0) {
			
				if(tag_tag.containsKey(a)){
					Hashtable<String, DeliciousEdgeSimple> temp = tag_tag.get(a);
					
					temp.put(b, edge);
					tag_tag.put(a,temp);
					
				}else{
					
					Hashtable<String, DeliciousEdgeSimple> temp = new Hashtable<String,DeliciousEdgeSimple>();
					
					temp.put(b,edge);
					
					tag_tag.put(a,temp);
				}
				
			}else{
				
				if(tag_tag.containsKey(b)){
					Hashtable<String, DeliciousEdgeSimple> temp = tag_tag.get(b);
					
					temp.put(a, edge);
					tag_tag.put(b,temp);
					
				}else{
					
					Hashtable<String, DeliciousEdgeSimple> temp = new Hashtable<String,DeliciousEdgeSimple>();
					
					temp.put(a,edge);
					
					tag_tag.put(b,temp);
				}
				
				
				
				
			}
		}
		

	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		String path = "../data/graph/last/delcious_graph_merged_enriched.txt";
		path = "../data/graph/last/sample.txt";
		
		trimByFreq(path);
		
		
		
	}

	


}


class TagFreq{
	String tag=null;
	
	int id=0;
	int freq=0;
	int freq_adults=0;
	
}
